#include "test_common.h"
TEST_START
#include "PikaCompiler.h"
// TEST(packtool, unpack) {

// PIKA_RES res = pikafs_unpack_files("test/out/packout/a0424.pack",
// "test/out/unpackout/");

//     EXPECT_EQ(res, PIKA_RES_OK);
// }

TEST(packtool, packfiles) {
    PikaMaker* maker = New_PikaMaker();
    PIKA_RES ret = PIKA_RES_OK;

    pikaMaker_linkRawWithPath(maker, "test/assets/test.txt", "/txt-file");
    pikaMaker_linkRawWithPath(maker, "test/assets/test.jpg", "/bmp-file");

    // create "./test/out/packout" path if not exist
    ret = pikaMaker_linkCompiledModulesFullPath(
        maker, "./test/out/packout/a0424.pack");

    pikaMaker_deinit(maker);
    EXPECT_EQ(ret, PIKA_RES_OK);
}

TEST(packtool, packfiles_txt) {
    PikaMaker* maker = New_PikaMaker();
    PIKA_RES ret = PIKA_RES_OK;

    pikaMaker_linkRawWithPath(maker, "test/assets/test.txt", "txt-file");
    pikaMaker_linkRawWithPath(maker, "test/assets/widget_config.ini",
                              "/widget_config.ini");

    // create "./test/out/packout" path if not exist
    ret = pikaMaker_linkCompiledModulesFullPath(maker,
                                                "./test/out/packout/ptxt.pack");

    pikaMaker_deinit(maker);
    EXPECT_EQ(ret, PIKA_RES_OK);

    pikafs_FILE* pack_file =
        pikafs_fopen_pack("test/out/packout/ptxt.pack", "widget_config.ini");
    EXPECT_TRUE(pack_file != NULL);
    while (1) {
        char buf = 0;
        size_t n = pikafs_fread(&buf, 1, 1, pack_file);
        if (n <= 0) {
            break;
        }
        /* the txt file should not contain '\0' */
        EXPECT_NE(buf, 0);
    }
    pikafs_fclose(pack_file);
    EXPECT_EQ(pikaMemNow(), 0);
}

TEST(packtool, packread_) {
    size_t n = 0;
    // Arg* fileArg = NULL;
    pikafs_FILE* pack_file =
        pikafs_fopen_pack("test/out/packout/a0424.pack", "test.txt");
    if (NULL == pack_file) {
        pika_platform_printf("open file: %s error\r\n",
                             "test/out/packout/a0424.pack");
    }

    FILE* file =
        pika_platform_fopen("test/out/unpackout/file3_test.txt", "wb+");
    if (NULL == file) {
        pika_platform_printf("open file: %s error\r\n",
                             "test/out/unpackout/file3_test.txt");
    }

    n = pika_platform_fwrite(pack_file->addr, pack_file->size, 1, file);

    arg_deinit(pack_file->farg);
    pikaFree(pack_file, sizeof(pikafs_FILE));
    pika_platform_fclose(file);
    pack_file = NULL;
    EXPECT_NE(n, 0);
}

TEST(packtool, packreadErr) {
    pikafs_FILE* pack_file =
        pikafs_fopen_pack("test/out/packout/0425.pack", "file3.txt");
    if (NULL == pack_file) {
        pika_platform_printf("open file: %s error\r\n",
                             "test/out/packout/a0424.pack");
    }

    EXPECT_TRUE(!pack_file);
}

TEST(packtool, packread) {
    size_t n = 0;
    // Arg* fileArg = NULL;
    pikafs_FILE* pack_file =
        pikafs_fopen_pack("test/out/packout/a0424.pack", "test.txt");
    if (NULL == pack_file) {
        pika_platform_printf("open file: %s error\r\n",
                             "test/out/packout/a0424.pack");
    }

    char buffer[1024] = {0};
    n = pikafs_fread(buffer, 1, 1024, pack_file);
    pikafs_fclose(pack_file);
    pack_file = NULL;
    EXPECT_NE(n, 0);
    EXPECT_EQ(pikaMemNow(), 0);
}

TEST_END
